package com.apkfuns.jsbridge;


import com.apkfuns.jsbridge.common.IPromptResult;
import com.apkfuns.jsbridge.common.IWebView;
import com.apkfuns.jsbridge.module.JsModule;
import ohos.agp.components.webengine.JsMessageResult;
import ohos.agp.components.webengine.JsTextInputResult;
import ohos.agp.components.webengine.WebView;
import ohos.app.Context;
import org.jetbrains.annotations.NotNull;

/**
 * Created by pengwei on 2017/6/8.
 */

public abstract class JsBridge {

    public static final String TAG = "JsBridgeDebug";

    public abstract void injectJs(@NotNull Context context, @NotNull WebView webView);
    public abstract void injectJs(@NotNull WebView webView);
    public abstract void injectJs(@NotNull Context context,@NotNull IWebView webView);
    public abstract void injectJs(@NotNull IWebView webView);

    public abstract boolean callJsPrompt(@NotNull String methodArgs, @NotNull JsTextInputResult
            result);

    public abstract boolean callJsPrompt(@NotNull String methodArgs, @NotNull JsMessageResult
            result);

    public abstract boolean callJsPrompt(@NotNull String methodArgs, @NotNull IPromptResult result);


    public abstract boolean onJsPrompt(WebView view, String url, String message,
                                       String defaultValue, JsTextInputResult
                                               result);

    public abstract void evaluateJavascript(@NotNull String jsCode);

    public abstract void clean();

    public abstract void release();


    /**
     * Recommended Use loadModule(JsModule...modules)
     */
    @Deprecated
    public static JsBridge loadModule(Class<? extends JsModule>... modules) {
        JsModule[] newModule = null;
        if (modules != null) {
            newModule = new JsModule[modules.length];
            for (int i = 0; i < newModule.length; i++) {
                try {
                    newModule[i] = modules[i].newInstance();
                } catch (Exception e) {
                    LogUtils.error("JsBridge","JSONException  " + e);
                }
            }
        }
        return new JsBridgeImpl(newModule);
    }

    public static JsBridge loadModule(JsModule...modules) {
        return new JsBridgeImpl(modules);
    }

    public static JsBridge loadModule() {
        return new JsBridgeImpl();
    }

    public static JsBridge loadModule(@NotNull String protocol, @NotNull String readyMethod, JsModule...modules) {
        return new JsBridgeImpl(protocol, readyMethod, modules);
    }
}
